// function once(fn) {
//   let called = false;
//   let result;

//   return function (...args) {
//     if (!called) {
//       called = true;
//       result = fn.apply(this, args);
//     }
//     return result;
//   };
// }

// // 示例用法
// const getExpensiveData = once(function (a) {
//   console.log("Fetching expensive data...");
//   return { data: "expensive data", a };
// });

// console.log(getExpensiveData("hello")); // 输出: Fetching expensive data... { data: 'expensive data' }
// console.log(getExpensiveData()); // 输出: { data: 'expensive data' } (不重新获取数据)

// function onceV(fn) {
//   let onceFLag = false;
//   let result;
//   return function (...args) {
//     if (!onceFLag) {
//       onceFLag = true;
//       result = fn.apply(this, args);
//     }
//     return result;
//   };
// }
// const getExpensiveData = onceV(function (a) {
//   console.log(this,'aa');
//   return { data: "expensive data", a };
// });
// console.log(getExpensiveData("hello"));
// console.log(getExpensiveData("hello"));

// console.log(obj.a("hello"));

function once(fn) {
  let called = false;
  let result;

  return function (...args) {
    if (!called) {
      called = true;
      result = fn.apply(this, args);
    }
    return result;
  };
}
const getExpensiveData = once(function (a) {
  console.log(this, "aa");
  return { data: "expensive data", a };
});
console.log(getExpensiveData("hello1"));
console.log(getExpensiveData("hello2"));
